package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.AdminMapper;
import com.whz.workmanageol.dao.StudentMapper;
import com.whz.workmanageol.dao.TeacherMapper;
import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.service.UpdateUserInfoService;
import com.whz.workmanageol.utils.VerifyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UpdateUserInfoServiceImpl implements UpdateUserInfoService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean updataEmail(User user, String newemail) {
        String id = user.getId();
        if(!VerifyUtils.isLegalEmail(newemail))
            return false;
        String role = user.getRole();
        if("student".equals(role)){
            studentMapper.updateEmailByID(id, newemail);
        }
        else if("teacher".equals(role)){
            teacherMapper.updateEmailByID(id, newemail);
        }
        else if("admin".equals(role)){
            adminMapper.updateEmailByID(id, newemail);
        }
        return true;

    }

    @Override
    public boolean updataTel(User user, String newetel) {
        String id = user.getId();
//        if(!VerifyUtils.isLegalTel(newetel))
//            return false;
        String role = user.getRole();
        if("student".equals(role)){
            studentMapper.updateTelByID(id, newetel);
        }
        else if("teacher".equals(role)){
            teacherMapper.updateTelByID(id, newetel);
        }
        else if("admin".equals(role)){
            adminMapper.updateTelByID(id, newetel);
        }
        return true;
    }

    @Override
    public boolean updataPassword(User user, String newpsw) {
        String id = user.getId();
        String role = user.getRole();
        if("student".equals(role)){
            studentMapper.updatePasswordByID(id, newpsw);
        }
        else if("teacher".equals(role)){
            teacherMapper.updatePasswordByID(id, newpsw);
        }
        else if("admin".equals(role)){
            adminMapper.updatePasswordByID(id, newpsw);
        }
        return true;
    }
}
